package com.example.jegotrip.adapter;

import static android.view.View.GONE;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;
import com.example.jegotrip.entity.NoteInfo;

import java.util.ArrayList;
import java.util.List;

public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.NoteListViewHolder> {

    List<NoteInfo> noteInfos = new ArrayList<>();

    @SuppressLint("NotifyDataSetChanged")
    public NoteListAdapter(List<NoteInfo> noteInfos) {
        this.noteInfos = noteInfos;
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")

    public void setNoteInfos(List<NoteInfo> noteInfos) {
        this.noteInfos = noteInfos;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public NoteListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//      onCreateViewHolder  ViewGroup parent   参数要传null就不会导致仅仅进一次onCreateViewHolder方法，就可以展示全数据
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_list_view,null,false);
        //点击noteInfo 查看详情页面进行传值
        NoteListViewHolder holder = new NoteListViewHolder(view);

        return holder;
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull NoteListViewHolder holder, int position) {
        NoteInfo noteInfo = noteInfos.get(position);
//        设置数据
        holder.ve_note_title.setText(noteInfo.getNote_title());
        holder.ve_note_content.setText(noteInfo.getNote_content());
        holder.note_create_time.setText(noteInfo.getNote_create_time());
        String[] split = noteInfo.getNote_create_time().split("-");
        holder.note_day.setText(split[2]);
        holder.note_month.setText(split[1] + "月");

        if (noteInfo.getNote_is_top() == 1){
            holder.note_is_top.setVisibility(View.VISIBLE);
            Typeface typeface = Typeface.create(Typeface.DEFAULT,Typeface.BOLD);
            holder.ve_note_title.setTypeface(typeface);
            holder.ve_note_content.setTypeface(typeface);
        } else {
            holder.note_is_top.setVisibility(GONE);
            Typeface typeface = Typeface.create(Typeface.DEFAULT,Typeface.NORMAL);
            holder.ve_note_title.setTypeface(typeface);
            holder.ve_note_content.setTypeface(typeface);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mNoteListOnItemClickListener != null){
                    mNoteListOnItemClickListener.onItemClick(position,noteInfo);
                }
            }
        });

        holder.note_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mNoteListOnItemClickListener != null){
                    mNoteListOnItemClickListener.onItemMoreClick(position,noteInfo);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return noteInfos.size();
    }

    static class NoteListViewHolder extends RecyclerView.ViewHolder{
        // 添加ViewHolder中的控件
        TextView ve_note_title;
        TextView ve_note_content;
        TextView note_create_time;
        TextView note_month;
        TextView note_day;
        TextView note_is_top;
        ImageView note_more;
        public NoteListViewHolder(@NonNull View itemView) {
            super(itemView);
            ve_note_title = itemView.findViewById(R.id.ve_note_title);
            ve_note_content = itemView.findViewById(R.id.ve_note_content);
            note_create_time = itemView.findViewById(R.id.note_create_time);
            note_month = itemView.findViewById(R.id.note_month);
            note_day = itemView.findViewById(R.id.note_day);
            note_more = itemView.findViewById(R.id.note_more);
            note_is_top = itemView.findViewById(R.id.note_is_top);
        }
    }

    public void setmNoteListOnItemClickListener(NoteListOnItemClickListener mNoteListOnItemClickListener) {
        this.mNoteListOnItemClickListener = mNoteListOnItemClickListener;
    }

    private NoteListOnItemClickListener mNoteListOnItemClickListener;

    public interface NoteListOnItemClickListener {

        void onItemClick(int position, NoteInfo noteInfo);
        void onItemMoreClick(int position, NoteInfo noteInfo);
    }
}
